package zhangqi.com.fragment_demo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class Fragment01 extends Fragment {

    //返回的view对象会作为fragment01的内容显示在屏幕上
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment01, null);

        final EditText et = (EditText) v.findViewById(R.id.et_flag);
        Button bt = (Button) v.findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = et.getText().toString();
                //把数据传递给Activity
                //这里很重要，getActivity()能获取Activity,但不是MainActivity，
                // 所以强转，不然无法调用setFgText1()方法
                ((MainActivity)getActivity()).setFgText1(text);
            }
        });
        return v;
    }
}
